'use strict';
app.controller('promotionPosterAddCtrl', ['$scope', 'toaster', 'utilService', 'FileUploader', '$cookies', 'promotionPosterAddService', '$state', '$timeout', '$modal', '$filter', '$stateParams', function ($scope, toaster, utilService, FileUploader, $cookies, promotionPosterAddService, $state, $timeout, $modal, $filter, $stateParams) {
    $scope.typeObject = [
        {name: '商品', value: '0'},
        {name: '店铺', value: '1'},
        {name: '平台', value: '2'},
    ];

    $scope.promotionPosterVO = {
        degree: 0,
        alpha: 1.0,
        tLocation: 10.0,
        lLocation: 10.0,
        sessionId: $cookies.sessionId,
        isDownload: 0,
        type: $scope.typeObject[0].value,
        pmWidth:500,
        pmHeight:700
    };
    $scope.uploadPictureStatus=true;
    $scope.checkedStatus = function (status) {
        $scope.promotionPosterVO.status = status;
        $scope.uploadPictureStatus = false;
    }
    //查询推广海报设置信息
    $scope.query = function () {
        if (angular.isUndefined($stateParams.id) || $stateParams.id == "") {
            return;
        }
        $scope.promotionPosterVO.id = $stateParams.id
        promotionPosterAddService.query($scope).then(function (rlt) {
            switch (rlt.status) {
                case 200:
                    $scope.promotionPosterVO = rlt.data;
                    $scope.promotionPosterVO.type = $scope.typeObject[rlt.data.type].value;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
    $scope.query();

    //图片上传
    var uploader = $scope.uploader = new FileUploader({
        url: $scope.app.domain + '/upload',
        formData: [{sessionId: $cookies.sessionId}]
    });
    uploader.filters.push({
        name: 'picFilter',
        fn: function (item, options) {
            if (!(item.type == 'image/png' || item.type == 'image/gif' || item.type == 'image/jpeg' || item.type == 'image/jpg')) {
                toaster.pop('waiting', '添加图片失败', '图片类型不符合');
                return false;
            }
            if (item.size > 2 * 1024 * 1024) {
                toaster.pop('waiting', '添加图片失败', '文件大小超出2M');
                return false;
            }
            if (this.queue.length > $scope.promotionPosterVO.queueLimit) {
                toaster.pop('waiting', '添加图片失败', '超出图片上传数量');
                return false;
            }
            return true;
        }
    });
    uploader.onAfterAddingFile = function (item) {// 添加文件到上传队列后
        if ($scope.promotionPosterVO.queueLimit < uploader.queue.length) {
            toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
            uploader.queue.splice(uploader.queue.indexOf(fileItem), 1);
        }
    }
    uploader.onWhenAddingFileFailed = function (item, filter, options) {//添加文件到上传队列失败后
    }
    uploader.onAfterAddingAll = function (addedItems) {//添加所选的所有文件到上传队列后
        if (uploader.getNotUploadedItems().length < 1) {
            return;
        }
        uploader.uploadAll();
    }
    uploader.onBeforeUploadItem = function (item) {//文件上传之前
    }
    uploader.onProgressItem = function (item, progress) {//文件上传中
    }
    uploader.onProgressAll = function (progress) {//上传队列的所有文件上传中
    }
    uploader.onSuccessItem = function (item, response, status, headers) {//文件上传成功后
        switch (status) {
            case 200:
                if ($scope.promotionPosterVO.status == 0) {
                    $scope.promotionPosterVO.srcImg = response.data;
                    $scope.uploadPictureStatus=true;
                }
                if ($scope.promotionPosterVO.status == 1) {
                    var img = new Image();
                    img.src = $scope.app.image.temp + response.data;
                    img.onload = function () {
                        $scope.promotionPosterVO.widthSize = img.width;
                        $scope.promotionPosterVO.heightSize = img.height;
                        $scope.promotionPosterVO.icon = response.data;
                        $scope.uploadPictureStatus=true;
                    }
                }
                break;
            default:
                break
        }
    }
    uploader.onErrorItem = function (item, response, status, headers) {//文件上传失败后
        toaster.pop('waiting', '上传图片失败', '未知错误');
    }
    uploader.onCancelItem = function (item, response, status, headers) {//文件上传取消后
    }
    uploader.onCompleteItem = function (item, response, status, headers) {//文件上传完成后
    }
    uploader.onCompleteAll = function () {//上传队列的所有文件上传完成后
    }
    //水印变小
    $scope.reduceSize = function () {
        if ($scope.promotionPosterVO.widthSize > 10 && $scope.promotionPosterVO.heightSize > 10) {
            $scope.promotionPosterVO.widthSize *= 0.9;
            $scope.promotionPosterVO.heightSize *= 0.9;
            $scope.promotionPosterVO.widthSize=$filter('number')($scope.promotionPosterVO.widthSize,0);
            $scope.promotionPosterVO.heightSize=$filter('number')($scope.promotionPosterVO.heightSize,0);
        }
        if ($scope.promotionPosterVO.widthSize < 10) {
            $scope.promotionPosterVO.widthSize = 10;
        }
        if ($scope.promotionPosterVO.heightSize < 10) {
            $scope.promotionPosterVO.heightSize = 10;
        }
    }
    //水印变大
    $scope.increaseSize = function () {
        if ($scope.promotionPosterVO.widthSize < 600 && $scope.promotionPosterVO.heightSize < 600) {
            $scope.promotionPosterVO.widthSize *= 1.1;
            $scope.promotionPosterVO.heightSize *= 1.1;
            $scope.promotionPosterVO.widthSize=$filter('number')($scope.promotionPosterVO.widthSize,0);
            $scope.promotionPosterVO.heightSize=$filter('number')($scope.promotionPosterVO.heightSize,0);
        }
        if ($scope.promotionPosterVO.widthSize > 600) {
            $scope.promotionPosterVO.widthSize = 600;
        }
        if ($scope.promotionPosterVO.heightSize > 600) {
            $scope.promotionPosterVO.heightSize = 600;
        }
    }
    //水印上下移动
    $scope.reduceTLocation = function () {
        $scope.promotionPosterVO.tLocation -= 10;
    }
    $scope.increaseTLocation = function () {
        $scope.promotionPosterVO.tLocation += 10;

    }
    //水印左右移动
    $scope.reduceLLocation = function () {
        $scope.promotionPosterVO.lLocation -= 10;
    }
    $scope.increaseLLocation = function () {
        $scope.promotionPosterVO.lLocation += 10;
    }
    //水印透明度值降低
    $scope.reduceAlpha = function () {
        if ($scope.promotionPosterVO.alpha > 0) {
            $scope.promotionPosterVO.alpha -= 0.05;
        }
        if ($scope.promotionPosterVO.alpha < 0) {
            $scope.promotionPosterVO.alpha = 0.0;
        }
    }
    //水印透明度值增加
    $scope.increaseAlpha = function () {
        if ($scope.promotionPosterVO.alpha < 1) {
            $scope.promotionPosterVO.alpha += 0.05;
        }
        if ($scope.promotionPosterVO.alpha > 1) {
            $scope.promotionPosterVO.alpha = 1.0;
        }
    }
    //水印顺时针旋转
    $scope.reduceDegree = function () {
        $scope.promotionPosterVO.degree -= 5;
    }
    //水印逆时针旋转
    $scope.increaseDegree = function () {
        $scope.promotionPosterVO.degree += 5;
    }
    //移动水印
    $scope.moveIcon = function (e) {
        if ($scope.moveStatus) {
            var divX = document.getElementById('posterDiv').getBoundingClientRect().x;
            var divY = document.getElementById('posterDiv').getBoundingClientRect().y;
            $scope.promotionPosterVO.lLocation = e.clientX - divX - $scope.promotionPosterVO.widthSize / 2;
            $scope.promotionPosterVO.tLocation = e.clientY - divY - $scope.promotionPosterVO.heightSize / 2;
        }
    }
    //确定
    $scope.confirm = function () {
        $scope.promotionPosterVO.lLocation = $filter('number')($scope.promotionPosterVO.lLocation, 0);
        $scope.promotionPosterVO.tLocation = $filter('number')($scope.promotionPosterVO.tLocation, 0);
        $scope.confirmStatus = true;
        $scope.promotionPosterVO.isDownload = 0;
        promotionPosterAddService.waterMark($scope).then(function (rlt) {
            switch (rlt.status) {
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.promotionPosterVO.fileName = rlt.data.fileName;
                    $scope.promotionPosterVO.srcImg=rlt.data.srcImg;
                    $scope.promotionPosterVO.icon=rlt.data.icon;
                    $scope.updateData();
                    $scope.confirmStatus = false;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.confirmStatus = false;
                    break;
            }
        });
    }
    //跟新海报表格信息
    $scope.updateData = function () {
        $scope.promotionPosterVO.lLocation = $filter('number')($scope.promotionPosterVO.lLocation, 0);
        $scope.promotionPosterVO.tLocation = $filter('number')($scope.promotionPosterVO.tLocation, 0);
        promotionPosterAddService.updateData($scope).then(function (rlt) {
            switch (rlt.status) {
                case 200:
                    $state.go('app.promotionPoster');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
    //下载海报
    $scope.download = function () {
        $scope.promotionPosterVO.lLocation = $filter('number')($scope.promotionPosterVO.lLocation, 0);
        $scope.promotionPosterVO.tLocation = $filter('number')($scope.promotionPosterVO.tLocation, 0);
        $scope.promotionPosterVO.isDownload = 1;
        $scope.promotionPosterVO.sessionId = $cookies.sessionId;
        openModal("确定下载推广海报吗?");
        function openModal(msg) {
            utilService.modal($modal, $scope, {
                title: "温馨提示",
                controller: "downloadPromotionPosterCtrl",
                message: msg,
                scope: $scope,
                data: $scope.promotionPosterVO
            });
        }
    }
}]);
app.controller('downloadPromotionPosterCtrl', ['$scope', '$modalInstance', 'modalData', 'promotionPosterAddService', function ($scope, $modalInstance, modalData, promotionPosterAddService) {
    $scope.modal = modalData;
    $scope.param = modalData.data;
    $scope.AllowToDelete = true;
    $scope.ok = function () {
        if (!$scope.AllowToDelete) {
            return;
        }
        $scope.AllowToDelete = false;
        promotionPosterAddService.download($scope);
        $modalInstance.dismiss('success');
    }
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    }
}]);